package com.bjpowernode.javase.collection;

import java.util.TreeSet;

public class TreeSetTest04 {
    public static void main(String[] args) {
        Persion1 p1 = new Persion1(20);
        Persion1 p2  = new Persion1(10);
        Persion1 p3 = new Persion1(32);

        TreeSet<Persion1> p = new TreeSet<>();
        p.add(p1);
        p.add(p2);
        p.add(p3);

        for (Persion1 pp : p){
            System.out.println(pp);
        }
    }
}

class Persion1 implements Comparable <Persion1>{
    int  age;
    public Persion1(){}
    public Persion1(int age){this.age = age;}
    public void setAge(int age){this.age = age;}
    public int getAge(){return age;}

    //重写toString()方法
    public String toString(){
        return "Persion[age="+age+"]";
    }

    /*
    需要在这个方法中编写比较规则。
     */

    @Override
    public int compareTo(Persion1 o) {
        //c1.compareTO(c2)
        //this = c1;o = c2
        //return结果大于零往集合的上面放。
        return  o.age - this.age ;
    }
}
